/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.lego.controller;

import lejos.nxt.LightSensor;
import lejos.nxt.MotorPort;
import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.SensorPort;

/**
 * The Singleton Class {@link SensorActorController} is responsible to connect
 * to Lego JVM Sensors (such as {@link LightSensor}) and Actors (such as
 * {@link MotorPort}) to their corresponding ports. This class encapsulates the
 * Lego Sensors and Actors and decorates their functions<br/>
 * <br/>
 * 
 * <strong>Note: </strong>It is very important for the Lego JVM that their
 * Actors and Sensors are initialized only once! Otherwise the Lego JVM runs
 * into problems. That is why we decided us the create the
 * {@link SensorActorController} and make it a Singleton<br/>
 * <br/>
 * <strong>Note: </strong>We had to make sure that the Class names don't exceed
 * the Lego JVM limit of 20 characters
 * 
 */
public class SensorActorController {
	private static SensorActorController sensorController;

	private int lightMax = 850;

	private int lightMin = 150;

	private NXTRegulatedMotor motorLeft = null;

	private NXTRegulatedMotor motorRight = null;

	private LightSensor lightSensorLeft = null;

	private LightSensor lightSensorRight = null;

	public static SensorActorController getInstance() {
		if (sensorController == null) {
			sensorController = new SensorActorController();
		}
		return sensorController;
	}

	public static SensorActorController getSensorController() {
		return sensorController;
	}

	private SensorActorController() {
		motorLeft = new NXTRegulatedMotor(MotorPort.A);
		motorRight = new NXTRegulatedMotor(MotorPort.C);
		lightSensorLeft = new LightSensor(SensorPort.S1);
		lightSensorRight = new LightSensor(SensorPort.S4);
		lightSensorLeft.setHigh(lightMax);
		lightSensorLeft.setLow(lightMin);
		lightSensorRight.setHigh(lightMax);
		lightSensorRight.setLow(lightMin);
		lightSensorLeft.setFloodlight(false);
		lightSensorRight.setFloodlight(false);
	}

	public int getLightSensorLeftValue() {
		return this.lightSensorLeft.getNormalizedLightValue();
	}

	public int getLightSensorRightValue() {
		return this.lightSensorRight.getNormalizedLightValue();
	}

	public void setMotorLeftSpeed(int newSpeed) {
		this.motorLeft.forward();
		this.motorLeft.setSpeed(newSpeed);
	}

	public void setMotorRightSpeed(int newSpeed) {
		this.motorRight.forward();
		this.motorRight.setSpeed(newSpeed);
	}

}
